<template>
	<view class="container">
		<view class="address-list">
			<view-NoData v-if="addressList.length==0">
					
			</view-NoData>
			<view class="a-address" v-for="(item,index) in addressList" :key="index">
				<view class="left-text" :class="item.is_default? 'active':''" 
				@tap="selectTap(item.id, item.address)">
					<view class="name-tel">
						 {{item.name}} {{item.phone}}
					</view>
					<view class="address-box">
						<text v-if="item.is_default==1" style="background: #FFF0D9; margin-right: 8px; color: #FA9700; border-radius: 8px; padding: 4px 8px; font-size: 10px;">默认</text> {{item.address}}
					</view>
				</view>
				<view class="right-edit" @tap.stop="selectTap(item.id)"></view>
				<view style="margin-right: 10px;" @tap.stop="deleteAddess(item.id,index)">
					<u-icon color="#ccc" size="25px" name="trash"></u-icon>
				</view>
			</view>
		</view>
		<view style="height: 70px;background-color: #FFFFFF;"></view>
		<view style="position: fixed; z-index: 1; width: 90%; left: 5%; bottom: 20px;">
			<u-button @click="addAddess" type="warning" shape="circle">新增收货地址</u-button>
		</view>
		<!-- <view class="bottom-box">
			<view class="add-btn" @tap="addAddess">新增收货地址</view>
		</view> -->

	</view>
</template>

<script>
	import {getAddList,delteAdress} from "@/common/config/request.js"
	export default {
		data() {
			return {
				addressList: [],
				type: ''
			}
		},
		onShow() {
			// 获取地址列表
			this.getAddress()
		},
		onLoad(option) {
			if (option.type) {
				this.type = option.type
			}
		},
		methods: {
			getAddress(){
				// 获取地址列表
				getAddList().then((res)=>{
					console.log(res,'地址列表')
					if(res.data.code==1){
						this.addressList=res.data.data
					}
				})
			},
			deleteAddess(id,index){
				// console.log(id,index)
				var that=this
				uni.showModal({
					content:'是否删除该地址',
					success: (res) => {
						console.log(res)
						if(res.confirm){
							let data={
								id:id
							}
							delteAdress(data).then((res)=>{
								console.log(res)
								if(res.data.code==1){
									that.addressList.splice(index,1)
								}
							})
						}
					}
				})
			},
			selectTap(id, address) {
				if (this.type === 'xuyuan' && address) {
					let pages = getCurrentPages()
					let prevPage = pages[pages.length - 2]		
					prevPage.$vm.address = address
					prevPage.$vm.address_id = id
					uni.navigateBack({
					   delta: 1
					})
				} else {
					uni.navigateTo({
						url:'./Addadress?id='+id
					})
				}
				
				console.log("tap item id:" + JSON.stringify(id));
			},
			// editAddess(id) {
			// 	uni.navigateTo({
			// 		url:'./Addadress?id='+id
			// 	})
			// 	console.log("edit item id:" + id);
			// },
			addAddess() {
				uni.navigateTo({
					url:'./Addadress'
				})
				console.log("tap add new Address");
			}
		}
	}
</script>

<style>
	.container {
		background-color: #F2f2f2;
	}

	.address-list {
		width: 100%;
		background-color: #fff;
		margin-top: 20upx;
		padding-bottom: 100upx;
	}

	.address-list .a-address {
		width: 720upx;
		margin-left: 30upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30upx 0;
		border-bottom: 1upx solid #eee;
	}

	.a-address .left-text {
		width: 610upx;
		box-sizing: border-box;
		padding-left: 58upx;
	}

	.a-address .left-text.active {
		background: url() no-repeat left center;
		background-size: 28upx auto;
	}

	.a-address .left-text .name-tel {
		margin-bottom: 20upx;
	}

	.a-address .left-text .address-box {
		font-size: 24upx;
		color: #888888;
		line-height: 36upx;
	}

	.a-address .right-edit {
		width: 109upx;
		height: 100%;
		padding: 50upx 0 50upx 58upx;
		box-sizing: border-box;
		background: url() no-repeat 43upx center;
		background-size: 35upx auto;
	}
	
	.bottom-box {
		position: fixed;
		width: 100%;
		bottom: 0;
		font-size: 28upx;
		color: #000000;
		border-top: 1upx solid #eee;
		height: 100upx;
		line-height: 100upx;
		background: #fff url() no-repeat 705upx center;
		background-size: 15upx auto;
	}

	.bottom-box .add-btn {
		margin-left: 30upx;
		padding-left: 70upx;
		background: url() no-repeat left center;
		background-size: 40upx auto;
	}
</style>
